package com.thread.threadfirst.encrypt;

import com.google.common.base.Charsets;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.CharSet;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.print.DocFlavor;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 *
 */
public class Base64Test {

    private static String man="Man";
    private static String a="A";
    private static String bc="BC";

    public static void main(String[] args) throws IOException {

        System.out.println("===========sun BASE64Encoder============");
        BASE64Encoder base64Encoder=new BASE64Encoder();

        //编码
        String encoderMan = base64Encoder.encode(man.getBytes());
        String encoderA = base64Encoder.encode(a.getBytes());
        String encoderBC = base64Encoder.encode(bc.getBytes());

        System.out.println("Man base64结果为：" + encoderMan);
        System.out.println("A base64结果为：" + encoderA);
        System.out.println("BC base64结果为：" + encoderBC);

        BASE64Decoder base64Decoder=new BASE64Decoder();

        //解码
        byte[] decoderMan = base64Decoder.decodeBuffer(encoderMan);
        String man = new String(decoderMan, StandardCharsets.UTF_8);
        System.out.println("TWFu decode结果为：" + man);
        /*
        输出
        Man base64结果为：TWFu
        A base64结果为：QQ==
        BC base64结果为：QkM=
        TWFu decode结果为：Man
        */

        System.out.println("===========apache.commons.codec BASE64============");

        //编码
        byte[] encodeBase64=Base64.encodeBase64(man.getBytes());
        String encodeMan = new String(encodeBase64, StandardCharsets.UTF_8);
        System.out.println("Man base64结果为：" + encodeMan);

        //解码
        byte[] decodeBase64 = Base64.decodeBase64(encodeBase64);
        String decodeMan = new String(decodeBase64, StandardCharsets.UTF_8);
        System.out.println("TWFu decode结果为：" + decodeMan);
        /*
        输出
        Man base64结果为：TWFu
        TWFu decode结果为：Man
        */

        String str="我";
        byte[] bytes=Base64.encodeBase64(str.getBytes());
        System.out.println("str encode: " + new String(bytes, Charsets.UTF_8));

        byte[] decodeBase641=Base64.decodeBase64(bytes);
        System.out.println("str decode: " + new String(decodeBase641,Charsets.UTF_8));

    }
}
